package com.wframe.project.former.dataservices.information;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import com.frame.database.BaseDao;
import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;

public class InformationService extends JBaseService<BaseDao>{

	public static InformationService getIntance(){
        return JServiceFactory.createJServiceProxy(InformationService.class);
    }
	
	/**
	 * 
	 * 功能:查询信息列表<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2016-12-7上午11:51:12<br>
	 * @throws Exception 
	 */
	
	public PageResult queryInfolList(JdataBean parms,int size,int pageno) throws Exception{
		String sql="select i.*,c.name channelname from wb_issue i,wb_channel c where i.code=c.code";
		List parmslist=new ArrayList();
		
		if(parms!=null){
			if(StringUtils.isNotBlank(parms.getString("code"))){
				sql+=" and i.code like ?";
				parmslist.add(parms.getString("code")+"%");
			}
			if(StringUtils.isNotBlank(parms.getString("title"))){
				sql+=" and i.title like ?";
				parmslist.add("%"+parms.getString("title")+"%");
			}
		}
		sql+=" order by i.orderid is null asc,i.orderid desc,i.id desc";
		return getDao().getSession().query2(sql, parmslist, size, pageno);
	}
	public void saveInfo(JdataBean bean) throws Exception{
		String c=bean.getString("content");
		bean.remove("content");
		if(StringUtils.isNotBlank(bean.getString("id"))){//修改记录
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			getDao().getSession().update("wb_issue", bean.getDataMap(),key);
			getDao().getSession().delete("delete from wb_issuecontent where ISSUEID=?",new Object[]{bean.getString("id")});
			if(StringUtils.isNotBlank(c)){
				addContent(bean.getString("id"),c);
			}
		}else{//新增记录
			bean.put("state", 1);//默认发布
			bean.put("issuetime", new Date());
			bean.put("createtime", new Date());
			String id=getDao().getSession().saveReturnKey("wb_issue", bean.getDataMap());
			if(StringUtils.isNotBlank(c)){
				addContent(id,c);
			}
		}
	}
	public void addContent(String id,String con) throws Exception{
		JdataBean cbean=new JdataBean();
		cbean.put("ISSUEID", id);
		cbean.put("PAGENUM", 1);
		cbean.put("CONTENT", con);
		getDao().getSession().save("wb_issuecontent", cbean.getDataMap());
	}
	public JdataBean getInfo(String id) throws Exception{
		return getDao().getSession().getObject("select i.*,c.name channelname from wb_issue i,wb_channel c where i.code=c.code and i.id=?", new Object[]{id});
	}
	public JdataBean getContent(String id) throws Exception{
		return getDao().getSession().getObject("select i.* from wb_issuecontent i where i.issueid=?", new Object[]{id});
	}
	public void delInfo(String id) throws Exception{
		getDao().getSession().delete("delete from wb_issue where id=?", new Object[]{id});
	}
	
	public List<JdataBean> loadChannel(String code) throws Exception{
		String sql="select code,id,pid,pid _parentId,name,photo,orderno from wb_channel where code like ? and code <>? order by orderno desc,id";
		return getDao().getSession().query(sql,new Object[]{code+"%",code});
	}
	/**
	 * 
	 * 功能:查询子栏目<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2016-12-7下午12:16:12<br>
	 * @throws Exception 
	 */
	public List<JdataBean> querySubChannel(String code) throws Exception{
		String c="";
		if(StringUtils.isNotBlank(code))c=code;
		return getDao().getSession().query("select * from wb_channel where code like ? order by code", new Object[]{c+"___"});
	}
	public String saveChannel(JdataBean bean) throws Exception{
		if(StringUtils.isBlank(bean.getString("id"))){
			JdataBean parbean=getChannelByCode(bean.getString("code"));
			bean.put("code", createChannelCode(bean.getString("code")));
			bean.put("pid", parbean.getString("id"));
			return getDao().getSession().saveReturnKey("wb_channel",bean.getDataMap());
		}else{
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			getDao().getSession().update("wb_channel",bean.getDataMap(),key);
			return bean.getString("id");
		}
	}
	
	
	
	public JdataBean getChannelByCode(String code) throws Exception{
		return getDao().getSession().getObject("select * from wb_channel where code=?", new Object[]{code});
	}
	
	public JdataBean getChannel(String id) throws Exception{
		return getDao().getSession().getObject("select * from wb_channel where id=?", new Object[]{id});
	}
	
	public void delChannel(String id) throws HibernateException, Exception{
		JdataBean bean=getChannel(id);
		getDao().getSession().delete("delete from wb_channel where id=?",new Object[]{id});
		getDao().getSession().delete("delete from wb_channel_product where channelcode like ?",new Object[]{bean.getString("code")+"%"});
	}
	
	private String createChannelCode(String parcode) throws Exception{
		int l=parcode.length()+1;
		List<JdataBean> list=getDao().getSession().query("select max(substr(m.code,"+l+",3)) code from wb_channel m where m.code like ?", new Object[]{parcode+"___"});
		String code=parcode+"001";
		if(list.size()>0 && StringUtils.isNotBlank(list.get(0).getString("code"))){
			int n=Integer.parseInt(list.get(0).getString("code"));
			n++;
			if(n<10)
				code= parcode+"00"+n;
			else if(n<100)
				code= parcode+"0"+n;
			else if(n>999)
				throw new Exception("主键序列号已超过3位");
			else
				code= parcode+n;
		}
		return code;
	}
	public PageResult queryChannelProduct(JdataBean parms,int size,int pageno) throws Exception{
		String sql="select t.*,CONCAT(p.id,'_',t.id) idcode,p.unitname,p.code productcode,p.name productname,p.attrvals,s.filepath photourl,s.viewpath photoview,sm.filepath product_photourl,sm.viewpath product_photoview"
				+ " from wb_channel_product f"
				+" inner join wb_products_prices t on t.id=f.priceid"
				+" inner join wb_products p on p.id=f.productid"
				+" left join app_pictures s on s.id=t.photo"//销售属性主图片
				+" left join app_pictures sm on sm.id=p.photo"//商品主图片
				+" where p.status<>-1";
		List parmlist=new ArrayList();
		String order="";
		if(parms!=null){
			if(StringUtils.isNotBlank(parms.getString("code"))){
				sql+=" and f.channelcode = ?";
				parmlist.add(parms.getString("code"));
			}
			if(StringUtils.isNotBlank(parms.getString("likecode"))){
				sql+=" and f.channelcode like  ?";
				parmlist.add(parms.getString("likecode")+"%");
			}
		}
		
		if(StringUtils.isNotBlank(order))sql+=" order by "+order;
		else sql+=" order by f.orderno";
		PageResult pr= getDao().getSession().query2(sql, parmlist, size, pageno);
		List<JdataBean> productlist=pr.getList();
		for(JdataBean product:productlist){
			String photo=product.getString("photoview");
			if(StringUtils.isBlank(photo))photo=product.getString("product_photoview");
			product.put("photo", photo);
			product.put("fullname", product.getString("productname")+" "+product.getDefault("name","").toString());
		}
		return pr;
	}
//	public void saveChannelProduct(String code,String proids) throws HibernateException, Exception{
//		getDao().getSession().delete("delete from wb_channel_product where channelcode=?",new Object[]{code});
//		if(StringUtils.isNotBlank(proids)){
//			String[] ids=proids.split(",");
//			for(String id:ids){
//				String[] ss=id.split("_");
//				getDao().getSession().update("insert into wb_channel_product(channelcode,productid,priceid) values (?,?,?)",new Object[]{code,ss[0],ss[1]});
//			}
//		}
//	}


		public void saveChannelProduct(String code,String proids) throws HibernateException, Exception{
		getDao().getSession().delete("delete from wb_channel_product where channelcode=?",new Object[]{code});
		if(StringUtils.isNotBlank(proids)){
			String[] ids=proids.split(",");
			for(int i=0;i<ids.length;i++){
				String[] ss=ids[i].split("_");
				getDao().getSession().update("insert into wb_channel_product(channelcode,productid,priceid,orderno) values (?,?,?,?)",new Object[]{code,ss[0],ss[1],i});
			}
		}
	}

	public PageResult queryStationnews(int size,int pageno) throws Exception{
		return getDao().getSession().query("select * from wb_stationnews order by id desc", null, size, pageno);
	}
	public void addStationnews(JdataBean bean) throws Exception{
		bean.put("create_time", new Date());
		String id=getDao().getSession().saveReturnKey("wb_stationnews",bean.getDataMap());
		getDao().getSession().update("insert into wb_stationnews_read(newsid) values (?)",new Object[]{id});
	}
	public JdataBean getStationnews(String id) throws Exception{
		return getDao().getSession().getObject("select * from wb_stationnews where id=?", new Object[]{id});
	}
	public void delStationnews(String id) throws HibernateException, Exception{
		getDao().getSession().delete("delete from wb_stationnews where id=?",new Object[]{id});
	}
	
	public PageResult querySmsinfo(int size,int pageno) throws Exception{
		return getDao().getSession().query("select * from wb_smsinfo order by id desc", null, size, pageno);
	}
	public void addSmsinfo(JdataBean bean) throws Exception{
		bean.put("create_time", new Date());
		getDao().getSession().saveReturnKey("wb_smsinfo",bean.getDataMap());
	}
	public JdataBean getSmsinfo(String id) throws Exception{
		return getDao().getSession().getObject("select * from wb_smsinfo where id=?", new Object[]{id});
	}
	public void delSmsinfo(String id) throws HibernateException, Exception{
		getDao().getSession().delete("delete from wb_smsinfo where id=?",new Object[]{id});
	}
}
